//
//  IncomeDetailViewController.m
//  XingHeHuiJu
//
//  Created by 于顺燊 on 2018/11/8.
//  Copyright © 2018年 cywkj. All rights reserved.
//

#import "IncomeDetailViewController.h"
#import "IncomeDetailTableViewCell.h"
#import "BRDatePickerView.h"

@interface IncomeDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *infoArr;

@end

@implementation IncomeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self createUI];
	self.page = 1;
	[self getInfoList];
	[self refreshData];
}

- (void)createUI {
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-50-50) style:UITableViewStylePlain];
	[_tableView registerNib:[UINib nibWithNibName:@"IncomeDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
	[self.view addSubview:_tableView];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	_tableView.backgroundColor = UISeparatedColorRGB;
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	IncomeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
	IncomeDetailInfo *model = self.infoArr[indexPath.row];
	cell.model = model;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	NSLog(@"i was here");
	return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"the number is %ld", self.infoArr.count);
	return self.infoArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		weakSelf.page = 1;
		[weakSelf getInfoList];
	}];
	
	_tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
		NSLog(@"上拉加载");
		weakSelf.page++;
		[weakSelf getInfoList];
	}];
	
}

- (void)getInfoList {
	if (!self.startTime) {
		self.startTime = [NSDate br_getDateString:[NSDate date] format:@"yyyy-MM-dd"];
	}
	if (!self.endTime) {
		self.endTime = [NSDate br_getDateString:[NSDate date] format:@"yyyy-MM-dd"];
	}
	NSDictionary *param = @{
							@"per_page":@"6",
							@"page":[NSString stringWithFormat:@"%ld", self.page],
							@"start_time":self.startTime,
							@"end_time":self.endTime
							};
	
	[[NetworkTool sharedTool] requestWithName:@"获取收入明细" URLString:APIIncomeDetails parameters:param method:@"POST" callBack:^(id responseObject) {
		
		NSArray *arr = responseObject[@"data"];
		if (self.page == 1) {
			[self.infoArr removeAllObjects];
		}
		for (NSDictionary *dict in arr) {
			
			IncomeDetailInfo *model = [[IncomeDetailInfo alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.infoArr addObject:model];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[self.tableView reloadData];
			[self.tableView.mj_header endRefreshing];
			[self.tableView.mj_footer endRefreshing];
		});
	}];
}

- (NSMutableArray *)infoArr {
	if (!_infoArr) {
		_infoArr = [[NSMutableArray alloc] init];
	}
	return _infoArr;
}
- (void)setStartTime:(NSString *)startTime
{
	if (_startTime != startTime) {
		_startTime = startTime;
	}
	[self getInfoList];
}

- (void)setEndTime:(NSString *)endTime
{
	if (_endTime != endTime) {
		_endTime = endTime;
	}
	[self getInfoList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
